const router = require('koa-router')(); //引入router 并调用
const {postLogData} =require("../utils/misc")
const ObjectId = require('mongodb').ObjectId

const mongo = require('../compents/mongo')
const koaMulter = require('@koa/multer')

// 设置根路径
router.prefix('/course')


// 测试用端口
router.get('/test', async ctx => {
    console.log(ctx);
    ctx.body = 'test'
})

// 获取后台数据
router.get('/list', async ctx => {
    postLogData("请求课程")
    let { limit, page, where } = ctx.query
    const res = await mongo.find('course', { limit: limit * 1, page: page * 1 })
    const total = await mongo.count('course', { where })
    ctx.body = { data: res, code: 20000, total, message: "端口求成功" }
})

// 保存数据
router.post('/save', async ctx => {
    postLogData("新增课程")
    const res = await mongo.insertOne('course', {...ctx.request.body })
    if (res.insertedId) {
        ctx.body = { data: res, code: 20000, message: "数据插入成功" };
    } else {
        ctx.body = { data: res, code: 50001, message: "数据插入失败" };
    }
})

// 修改数据
router.post('/change', async ctx => {
    postLogData("修改课程")
    const { _id } = ctx.request.body
    const update = ctx.request.body
    delete update._id
    const res = await mongo.updateOne('course', { where: { _id: ObjectId(_id) }, update })
    if (res.modifiedCount > 0) {
        ctx.body = { data: res, code: 20000, message: "数据修改成功" };
    } else {
        ctx.body = { data: res, code: 50002, message: "数据修改失败" };
    }

})

// 删除单挑数据
router.get('/delOne', async ctx => {
    postLogData("单删课程")
    const { _id } = ctx.query
    const res = await mongo.deleteOne('course', { _id: ObjectId(_id) })
    if (res.deletedCount > 0) {
        ctx.body = { data: res, code: 20000, message: "数据删除成功" };
    } else {
        ctx.body = { data: res, code: 50003, message: "数据删除失败" };
    }
})

// 删除多条数据
router.get('/delMany', async ctx => {
    postLogData("多删课程")
    let { ids } = ctx.query
    ids = JSON.parse(ids)
    const res = await mongo.deleteMany('course  ', ids)
    if (res.deletedCount > 0) {
        ctx.body = { data: res, code: 20000, message: "数据批量删除成功" };
    } else {
        ctx.body = { data: res, code: 50004, message: "数据批量删除失败" };
    }
})


// 文件上传配置
var storage = koaMulter.diskStorage({ //配置文件上传的方式   memoryStorage 内存的意思 把图片上传到内存里   diskStorage硬盘的意思 所有上传的文件都放到硬盘里
    //文件保存路径
    destination: (req, file, cb) => {
        cb(null, './uploads') //注意路径必须存在
    },
    //修改文件名称
    filename: (req, file, cb) => {
        const ext = file.originalname.split(".").pop()
        cb(null, 'course-' + Date.now() + "." + ext)
    }
})

//加载配置
var upload = koaMulter({ storage })

// 图片上传-单域单文件
router.post("/upload", upload.single('img'), async ctx => {
    ctx.body = { data: ctx.file.filename, msg: "文件上传成功", code: 20000 }
})


// 查询
router.get('/search', async ctx => {
    postLogData("搜索课程")
    let { id } = ctx.query
    let where = { _id: ObjectId(id) }
    const res = await mongo.findOne('course', where)
    console.log(res);
    ctx.body = { code: 20000, data: res, message: '接口请求成功' }
})


module.exports = router //导出路由对象